/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.image.Image;
import control.ControllerPrincipal;
import model.Zone;
import java.lang.Exception;
import tools.FileBrowser;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import model.Player;
import vinciappli.Templates;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.animation.transition.FadeTransition;
import javafx.geometry.HPos;
import model.SeaArea;
import javafx.util.Sequences;
import javafx.scene.layout.HBox;
import view.MenuManager;
import java.lang.Math;
import javafx.scene.transform.Transform;
import view.ConquestArrow;
import model.Province;
import vinciappli.StaticParams;
import java.lang.Integer;
import java.lang.String;
import view.PlayerSlot;
import javafx.scene.Cursor;
import vinciappli.CardId;
import view.InnerMenu;
import view.ZoneInteraction;
import javafx.animation.transition.TranslateTransition;
import view.ViewPrincipal;
import java.lang.System;

/**
 * @author Sik
 */
public class GameScreen extends CustomNode
{
    public var pieces: MapZone[];
    public var occupation : Pawn[];
    public var slots : PlayerSlot[];
    public var thumbnails : Thumbnail[];

    public var innerMenus : InnerMenu[];
    public-read def STRATEGIE : MenuStruct = MenuStruct{intitule : "STRATEGIE" val : 0 };
    public-read def ATTAQUE : MenuStruct = MenuStruct{intitule : "ATTAQUE" val : 1 };
    public-read def DEFENSE : MenuStruct = MenuStruct{intitule : "DEFENSE" val : 2 };
    public-read def REORGANISATION : MenuStruct = MenuStruct{intitule : "REORGANISATION" val : 3 };
    
    public var strategieMenu : InnerMenu[];
    public-read def DECLIN : MenuStruct = MenuStruct{intitule : "DECLIN EMPIRE" val : 0 };
    public-read def SELECTION : MenuStruct = MenuStruct{intitule : "SELECTION CIVILISATION" val : 1 };
    public-read def RECUPERER : MenuStruct = MenuStruct{intitule : "RECUPERER TROUPES" val : 2 };

    var selectedMenu : Integer;
    var selectedPiece : MapZone;
    var conquestArrows : ConquestArrow[];

    var nextStepBlocker : Boolean;

    var edgeOrigin : MapZone =
    MapZone
    {
        piece: ImageView
        {
        }
    }

    var zoneInteractPanel : ZoneInteraction =
    ZoneInteraction
    {
        translateX: 250
        translateY:500
    }

    public var civilizationPicker : CivilPlace =
    CivilPlace
    {
        blocksMouse: true
    }

    public var openCivPicker : ImageView =
    ImageView
    {
        image: Image
        {
            url : "file:///{{Templates.GUI_PATH}}bigger.png"
        }
        onMouseClicked: function(e:MouseEvent) : Void
        {
            civilizationPicker.magnified();
        }
        fitWidth: 25
        preserveRatio: true
        blocksMouse: true
        opacity: 0.7
        cursor: Cursor.HAND
    }

    var transitionTxt : String;

    public function iniGame(): Void
    {
        transitionTxt = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getCurrentPlayer().getNickname();
        buildPlayerSlot();
    }

    var transitionPanel : InnerMenu =
    InnerMenu
    {
        height: 200
        width: 450
        text: bind transitionTxt
        color: Color.WHITESMOKE
        opacity: 0.85
        fontSize: 50
        blocksMouse: true
        pickOnBounds: true
        cursor: Cursor.HAND
        onMouseClicked: function(e:MouseEvent) : Void
        {
            fadeMenu(transitionPanel, true, 0);
            transitionPanel.visible = false;
            startTurn();
        }

    }

    var exit : InnerMenu =
    InnerMenu
    {
        height: 20
        width: 50
        text: "EXIT"
        color: Color.GREY
        opacity: 0.7
        fontSize: 14
        blocksMouse: true
        pickOnBounds: true
        cursor: Cursor.HAND
        onMouseClicked: function(e:MouseEvent) : Void
        {
            System.exit(0);
            //ViewPrincipal.getInstance().nextScreen(this);
        }
    }

    var nextStep : InnerMenu =
    InnerMenu
    {
        height: 40
        width: 70
        text: "NEXT STEP"
        color: Color.BLUE
        opacity: 0.7
        fontSize: 15
        blocksMouse: true
        pickOnBounds: true
        cursor: Cursor.HAND
        onMouseClicked: function(e:MouseEvent) : Void
        {
            cleanDisplay();
            if(selectedMenu == REORGANISATION.val)
            {
                if(ControllerPrincipal.getINSTANCE().getControllerGameBoard().getIdleTroops().isEmpty())
                {
                    var p = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getMemoPlayer();
                    if(p != null)
                    {
                        slots[ControllerPrincipal.getINSTANCE().getControllerGameBoard().getNumCurrentPlayer()].setActive(false);
                        ControllerPrincipal.getINSTANCE().getControllerGameBoard().setCurrentPlayer(p);
                    }
                }
            }
            else
            {
                if(selectedMenu == DEFENSE.val and not(nextStepBlocker))
                {
                    java.lang.System.out.println("L'étape de défense n'est pas validée");
                }
                else
                {
                    selectedMenu += 1;
                }
            }
            nextStepBlocker = MenuManager.activeMenu(selectedMenu);
        }

    }

    public function destArrow(arrow : ConquestArrow, origin : MapZone, dest : MapZone) : Void
    {
        var adjustX : Number = 0;
        var adjustY : Number = 0;
        
        var xOri = origin.piece.x + origin.piece.fitWidth/2;
        var yOri = origin.piece.y + origin.piece.fitHeight/2;
        var xDest = dest.piece.x + dest.piece.fitWidth/2;
        var yDest = dest.piece.y + dest.piece.fitHeight/2;
        var adj = Math.abs(xDest - xOri);
        var opo = Math.abs(yDest - yOri);
        var hypo = Math.sqrt(Math.pow(adj, 2) + Math.pow(opo, 2));
        var cos = adj/hypo;
        var angle = Math.acos(cos)* 180/Math.PI;

        if(opo > 20)
        {
            adjustY = arrow.height*((yOri-yDest)/opo)*(opo/100);
        }
        if(adj > 20)
        {
            adjustX = arrow.width*((xOri-xDest)/adj)*(adj/100);
        }

        arrow.translateX = dest.piece.x + dest.piece.fitWidth/2 - arrow.width/2 + adjustX;
        arrow.translateY = dest.piece.y  + dest.piece.fitHeight/2 - arrow.height/2 + adjustY;

        
        if(xOri > xDest)
        {
            angle = 180 - angle;
        }
        if(yOri > yDest)
        {
            angle = 360 - angle;
        }
       
        arrow.transforms = Transform.rotate(angle, arrow.width/2, arrow.height/2);
        arrow.antiRotate(angle);
    }

    public function updateInteractPanel(currentPlayer : Player, selectedProv : Province) : Void
    {
        if((currentPlayer.getEmpire(true).getListProvinces().contains(selectedProv)) or currentPlayer.getAvailableTroops() <= 0)
        {
            zoneInteractPanel.interactions[ZoneInteraction.PLUS_BUTTON].isPossible = false;
        }
        else
        {
            zoneInteractPanel.interactions[ZoneInteraction.PLUS_BUTTON].isPossible = true;
        }

        if((currentPlayer.getEmpire(true).getListProvinces().contains(selectedProv)) or selectedProv.getTerritoryOccupation().get(currentPlayer.getNickname()) <= 1)
        {
            zoneInteractPanel.interactions[ZoneInteraction.MINUS_BUTTON].isPossible = false;
        }
        else
        {
            zoneInteractPanel.interactions[ZoneInteraction.MINUS_BUTTON].isPossible = true;
        }
    }

    

    public function buildGameBoard(): Void
    {
        var z: Zone;
        var num: Integer;
        var name: String;
        var scaleRatio: Number = 0.50;

        for (url in FileBrowser.listDirectory(Templates.ZONE_PATH))
        {
            name = url.substring(url.lastIndexOf("\\") + 1, url.lastIndexOf("."));
            try
            {
                num = Integer.parseInt(name);
                z = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getGameBoard().getZoneByNum(num);

                var pic =
                Image
                    {
                        url: "file:///{{url}}"
                    }

                var view : ImageView =
                ImageView
                {
                    onMouseClicked: function(e:MouseEvent) : Void
                    {
                        selectedPiece = findPiece(e.node);
                        cleanDisplay();
                        if(selectedMenu == ATTAQUE.val or selectedMenu == DEFENSE.val or selectedMenu == REORGANISATION.val)
                        {
                            var currentPlayer = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getCurrentPlayer();
                            var prov = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getGameBoard().getProvincesFromPlayer(currentPlayer);
                        if(ControllerPrincipal.getINSTANCE().getControllerGameBoard().getGameBoard().getZoneByNum(selectedPiece.num).getClass() != SeaArea.class)
                        {
                            var selectedProv : Province = (ControllerPrincipal.getINSTANCE().getControllerGameBoard().getGameBoard().getZoneByNum(selectedPiece.num)
                                as Province);
                            var startingCiv : Boolean = currentPlayer.getEmpire(false).getListProvinces().isEmpty();

                            if(prov.contains(selectedProv) and 
                            (selectedMenu == DEFENSE.val or not (ControllerPrincipal.getINSTANCE().getControllerGameBoard().getNewProvinces().contains(selectedProv))))
                            {
                                zoneInteractPanel.visible = true;
                                var panelX : Number;
                                var panelY : Number;

                                if(e.x + zoneInteractPanel.width > Templates.PLAYER_SLOT_WIDTH + Templates.MAP_WIDTH)
                                {
                                    panelX = Templates.PLAYER_SLOT_WIDTH + Templates.MAP_WIDTH - zoneInteractPanel.width - 25;
                                }
                                else
                                {
                                    panelX = view.x + pic.width*scaleRatio/2;
                                }

                                if(e.y + zoneInteractPanel.height > Templates.STAGE_HEIGHT)
                                {
                                    panelY = Templates.STAGE_HEIGHT - zoneInteractPanel.height - 25;
                                }
                                else
                                {
                                    panelY = view.y + pic.height*scaleRatio/2;
                                }

                                zoneInteractPanel.translateX = panelX;
                                zoneInteractPanel.translateY = panelY;

                                updateInteractPanel(currentPlayer, selectedProv);

                                zoneInteractPanel.interactions[ZoneInteraction.PLUS_BUTTON].onMouseClicked = function(e:MouseEvent) : Void
                                {
                                    if(zoneInteractPanel.interactions[ZoneInteraction.PLUS_BUTTON].isPossible)
                                    {
                                        if(selectedProv.setTroopsFromCurrentNumber(currentPlayer, 1))
                                        {
                                            globalUpdateDisplay();
                                            updateInteractPanel(currentPlayer, selectedProv);
                                        }
                                    }
                                }
                                zoneInteractPanel.interactions[ZoneInteraction.MINUS_BUTTON].onMouseClicked = function(e:MouseEvent) : Void
                                {
                                     if(zoneInteractPanel.interactions[ZoneInteraction.MINUS_BUTTON].isPossible)
                                    {
                                        if(selectedProv.setTroopsFromCurrentNumber(currentPlayer, -1))
                                        {
                                            globalUpdateDisplay();
                                            updateInteractPanel(currentPlayer, selectedProv);
                                        }
                                    }
                                }
                                zoneInteractPanel.interactions[ZoneInteraction.NONE_BUTTON].onMouseClicked = function(e:MouseEvent) : Void
                                {
                                    selectedProv.leave();
                                    globalUpdateDisplay();
                                    zoneInteractPanel.visible = false;
                                }
                            }
                            else
                            {
                                if(selectedMenu == ATTAQUE.val)
                                {
                                    var potentialProv = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getPotentialProvinces(currentPlayer);

                                    if(potentialProv.contains(selectedProv))
                                    {
                                        if(startingCiv)
                                        {
                                            edgeOrigin.piece.x = selectedPiece.piece.x ;
                                            edgeOrigin.piece.y = selectedPiece.piece.y ;
                                        }
                                        var originProv = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getGameBoard().getOriginProvFromDest(
                                        currentPlayer, selectedProv);
                                        for(origin in originProv)
                                        {
                                            var cost = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getConquestCost(origin, selectedProv,
                                            currentPlayer);
                                            var arr : ConquestArrow =
                                            ConquestArrow
                                            {
                                                width: 60
                                                height: 45
                                                strokeColor: PlayerSlot.colors[ControllerPrincipal.getINSTANCE().getControllerPlayer().getListPlayers().indexOf(currentPlayer)]
                                                fillColor: Color.WHITE
                                                cost: String.valueOf(cost);
                                                cursor: Cursor.HAND
                                                onMouseClicked: function(e:MouseEvent) : Void
                                                {
                                                    if(startingCiv)
                                                    {
                                                        if(ControllerPrincipal.getINSTANCE().selectFirstProvince(selectedProv))
                                                        {
                                                            cleanDisplay();

                                                        }
                                                        else
                                                        {
                                                            // TODO Affichage Conquete PAS OK
                                                            java.lang.System.out.println("PAS OK");
                                                            cleanDisplay();
                                                        }
                                                    }
                                                    else
                                                    {
                                                        // TODO Utilisation carte Espion!!!!!!!!!!!!!!!!!!
                                                        if(ControllerPrincipal.getINSTANCE().selectProvince(origin, selectedProv, false))
                                                        {
                                                            cleanDisplay();
                                                        }
                                                        else
                                                        {
                                                            // TODO Affichage Conquete PAS OK
                                                            java.lang.System.out.println("PAS OK");
                                                            cleanDisplay();
                                                        }
                                                    }

                                                    globalUpdateDisplay();
                                                }
                                            };
                                            insert arr into conquestArrows;
                                            if(startingCiv)
                                            {
                                                destArrow(arr, edgeOrigin, selectedPiece);
                                            }
                                            else
                                            {
                                                destArrow(arr, pieces[origin.getNum()-1], selectedPiece);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    }
                    onMouseEntered: function(e:MouseEvent) : Void
                    {
                        view.opacity = 0.5
                    }
                    onMouseExited: function(e:MouseEvent) : Void
                    {
                        view.opacity = 1
                    }
                    
                    x: Templates.PLAYER_SLOT_WIDTH*0.96 + 2 + z.getCoordinates().getX()
                    y: z.getCoordinates().getY()
                    fitHeight: pic.height * scaleRatio
                    fitWidth: pic.width * scaleRatio
                    image: pic

                };
                var zone : MapZone =
                MapZone
                {
                    num: num
                    piece: view
                }

                insert zone into pieces;

            } catch (e: Exception)
            {
//                println("Chargement d'un fichier qui n'est pas une pièce du Gameboard");
//                e.printStackTrace();
            }
        }
        sortArray();
    }

    public function cleanDisplay() : Void
    {
        delete conquestArrows;
        zoneInteractPanel.visible = false;
    }

    public function findPiece(element : Node) : MapZone
    {
        for(zone in pieces)
        {
            if(zone.piece == (element as ImageView))
            {
                return zone;
            }
        }
        return null;
    }

    public function getActiveSlot() : PlayerSlot
    {
        for(slot in slots)
        {
            if(slot.isActive)
            {
                return slot;
            }
        }
        return null;
    }

    public function globalUpdateDisplay() : Void
    {
        slotsUpdateDisplay();
        occupationDisplay();
    }


    public function slotsUpdateDisplay() : Void
    {
        for(slot in slots)
        {
            slot.updateDisplay();
        }
    }


    public function sortArray() : Void
    {
        pieces = Sequences.sort(pieces) as MapZone[];
    }

    public function buildPlayerSlot() : Void
    {
        var x : Number = 0;
        var y : Number = 0;
        var p : Player;
        var scaleRatio : Number = 0.96;

        for(s in [0..(ControllerPrincipal.getINSTANCE().getControllerPlayer().getListPlayers().size()-1)])
        {
            if(ControllerPrincipal.getINSTANCE().getControllerPlayer().getListPlayers().isEmpty() or
                ControllerPrincipal.getINSTANCE().getControllerPlayer().getListPlayers().get(s) == null)
            {
                p = null;
            }
            else
            {
                p = ControllerPrincipal.getINSTANCE().getControllerPlayer().getListPlayers().get(s);
            }

            insert
            PlayerSlot
            {
                p : p
                translateX: x -2
                translateY: y -3
                scaleX: scaleRatio
                scaleY: scaleRatio

            }
            into slots;
            y += Templates.PLAYER_SLOT_HEIGHT * scaleRatio;
            if(Templates.STAGE_HEIGHT - y <= Templates.PLAYER_SLOT_HEIGHT)
            {
                y = 0;
                x += Templates.MAP_WIDTH;
            }
        }

    }

    public function buildThumbnails() : Void
    {
        var x : Number = Templates.PLAYER_SLOT_WIDTH + 2;
        var y : Number = Templates.STAGE_HEIGHT - 43 - 20;
        var titles = ["INFOS", "AIDE", "ALERTE"];
        var i : Integer = 0;

        for(title in titles)
        {
            var tn : Thumbnail =
            Thumbnail
            {
                x: x
                y: y
                txt: title
                opacity: 0.65
                onMouseClicked: function(e:MouseEvent) : Void
                {
                    if(tn.isOpen)
                    {
                        openThumbnail(tn, false);
                        tn.isOpen = false;
                    }
                    else
                    {
                        closeThumbnails();
                        openThumbnail(tn, true);
                        tn.isOpen = true;
                    }
                }
            };
            tn.xPosition = 10 + i*(tn.width  + 10);
            insert tn into thumbnails;

            i++;
        }

    }

    public function closeThumbnails() : Void
    {
        for(tn in thumbnails)
        {
            if(tn.isOpen)
            {
                openThumbnail(tn, false);
            }
        }
    }


    public function openThumbnail(tn : Thumbnail, open : Boolean) : Void
    {
        var slide : TranslateTransition =
        TranslateTransition
        {
            duration: 0.6s
            node: tn
            toY : bind if(open) then Templates.STAGE_HEIGHT - 250 else Templates.STAGE_HEIGHT - 63
            repeatCount:1 autoReverse: false
        }
        slide.playFromStart();
    }


    public function buildMenu(options : String[], clickable : Boolean, colorChoice : Color) : InnerMenu[]
    {
        var menu : InnerMenu[];
        for(i in options)
        {
            var it : InnerMenu = InnerMenu
            {
                color: bind if (colorChoice == null) then InnerMenu.DEFAULT_COLOR else colorChoice
                text: i
                customCursor: bind if(clickable) then Cursor.HAND else Cursor.DEFAULT
            }
            insert it into menu;
        }
        return menu
    }

    public function fadeMenu(element : Node, hide : Boolean, stopValDef : Number) : Void
    {
        var iniVal : Number;
        var stopVal : Number;

        if(hide)
        {
            stopVal = 0;
        }
        else
        {
            element.visible = true;
            stopVal = 1;
        }
        if(stopValDef != 0)
        {
            stopVal = stopValDef;
        }

        iniVal = element.opacity;

        var fadeSelection =
        FadeTransition
        {
            duration: 1s
            node: element
            fromValue: iniVal toValue: stopVal
            repeatCount:1 autoReverse: false
        }
        fadeSelection.playFromStart();
    }

    public function occupationDisplay(): Void
    {
        var zones = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getGameBoard().getProvinces();
        var nb : Integer = 0;
        var player : Player;
        var num : Integer;
        var color : Color;

        delete occupation;
       
        for(zone in zones)
        {
            for(entry in zone.getTerritoryOccupation().entrySet())
            {
                nb = 0;
                if(entry.getKey().equals(StaticParams.PAWN_TYPE_FORT))
                {
                    var p =
                     Pawn
                     {
                        x: pieces[zone.getNum()-1].piece.x + pieces[zone.getNum()-1].piece.fitWidth/2 - 15;
                        y: pieces[zone.getNum()-1].piece.y + pieces[zone.getNum()-1].piece.fitHeight/2 - 15;
                        colorFill: Color.ANTIQUEWHITE
                        colorStroke: Color.BLACK
                        value: "F"
                        numZone: zone.getNum()-1;
                     }
                    insert p into occupation;
                    nb++;
                }
                else
                {
                    player = ControllerPrincipal.getINSTANCE().getControllerPlayer().getPlayerByName(entry.getKey());
                    num = ControllerPrincipal.getINSTANCE().getControllerPlayer().getListPlayers().indexOf(player);
                    color = PlayerSlot.colors[num];
                    java.lang.System.out.println(player.getNickname());
                    var troops =
                     Pawn
                     {
                        x: pieces[zone.getNum()-1].piece.x + pieces[zone.getNum()-1].piece.fitWidth/2 - 15;
                        y: pieces[zone.getNum()-1].piece.y + pieces[zone.getNum()-1].piece.fitHeight/2 - 15 + nb*30;
                        colorFill: Color.WHITE
                        colorStroke: color
                        value: String.valueOf(entry.getValue())
                        numZone: zone.getNum();
                     }
                    insert troops into occupation;
                }
            }
        }
    }

    public override function create(): Node
    {
        println("Build gameScreen");
        ini();

        Group
        {
            content: bind[
                Group
                {
                    content:
                    [
                        pieces,
                        edgeOrigin,
                        occupation,
                        slots
                    ]
                }
                
                transitionPanel,
                zoneInteractPanel,
                civilizationPicker,
                openCivPicker,
                conquestArrows,
                thumbnails,
               
                Rectangle
                {
                    x: Templates.PLAYER_SLOT_WIDTH + 17
                    y : 20
                    height: innerMenus[STRATEGIE.val].height * 4 + 40
                    width: innerMenus[STRATEGIE.val].width + 20
                    fill: Color.WHITESMOKE
                    arcHeight: 20
                    arcWidth: 20
                    opacity:0.7
                    blocksMouse: true
                }

                Group
                {
                    translateX: Templates.PLAYER_SLOT_WIDTH + 25
                    translateY: 25
                    
                    content :
                        [
                            HBox
                            {
                                content :
                                [
                                    VBox
                                    {
                                        spacing: 5
                                        content :
                                        [
                                            innerMenus[STRATEGIE.val],
                                            innerMenus[ATTAQUE.val],
                                            innerMenus[DEFENSE.val],
                                            innerMenus[REORGANISATION.val]
                                        ]
                                    }
                                    nextStep,
                                    

                                ]
                            }
                            exit,
                            VBox
                            {
                                translateY: innerMenus[STRATEGIE.val].height + 5
                                spacing: 5
                                content :
                                [
                                    strategieMenu[DECLIN.val],
                                    strategieMenu[SELECTION.val],
                                    strategieMenu[RECUPERER.val]
                                ]
                            }
                        ]
                }
            ]
        }
    }

    public function startTurn() : Void
    {
        var currentPlayer : Integer = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getNumCurrentPlayer();
        
        slots[currentPlayer].setActive(true);
        
        if(slots[currentPlayer].p.getEmpire(false).getCivilization().isComposedOf(CardId.diplomatie))
        {
            // TODO Demande de choix d'un allié
        }
        if(ControllerPrincipal.getINSTANCE().openingTurn(null) != 0)
        {
            slots[currentPlayer].updateDisplay();
        }
        
        if(ControllerPrincipal.getINSTANCE().getControllerGameBoard().getNbRound() != 0)
        {
            if(not civilizationPicker.isReduced)
            {
                civilizationPicker.setActive(false);
                civilizationPicker.miniaturized();
            }
            selectedMenu = STRATEGIE.val;
        }
        else
        {
            MenuManager.iniActionMenu();
            civilizationPicker.magnified();
            civilizationPicker.setActive(true);
        }

        MenuManager.activeMenu(selectedMenu);
    }

    public function closeTurn() : Void
    {
        var currentPlayer : Integer = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getNumCurrentPlayer();
        slots[currentPlayer].setActive(false);
        if(not civilizationPicker.isReduced)
        {
            civilizationPicker.setActive(false);
            civilizationPicker.miniaturized();

        }
        
        ControllerPrincipal.getINSTANCE().closingTurn();
        globalUpdateDisplay();
        nextPlayer();
    }

    public function nextPlayer() : Void
    {
        if(ControllerPrincipal.getINSTANCE().nextPlayer())
        {
            transitionTxt = "{{ControllerPrincipal.getINSTANCE().getControllerPlayer().getBestPlayer().getNickname()}}  WIN!";
            transitionPanel.onMouseClicked = function(e:MouseEvent) : Void
            {
                ViewPrincipal.getInstance().nextScreen(this);
            }
        }
        else
        {
            transitionTxt = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getCurrentPlayer().getNickname();
        }
        fadeMenu(transitionPanel, false, 0.85);
    }

    public function ini(): Void
    {
        
        println("GameScreen ini");
        buildGameBoard();
        buildThumbnails();

        innerMenus = buildMenu([STRATEGIE.intitule, ATTAQUE.intitule, DEFENSE.intitule, REORGANISATION.intitule], false, null);
        strategieMenu = buildMenu([DECLIN.intitule, SELECTION.intitule, RECUPERER.intitule], true, Color.WHITE);

        transitionPanel.translateX = Templates.STAGE_WIDTH / 2 - transitionPanel.width / 2;
        transitionPanel.translateY = Templates.STAGE_HEIGHT / 2 - transitionPanel.height / 2;
        nextStep.translateX = - innerMenus[STRATEGIE.val].width / 2 - nextStep.width/2;
        nextStep.translateY = innerMenus[STRATEGIE.val].height * 4 + 30;
        openCivPicker.translateX = Templates.STAGE_WIDTH / 2 - 75;
        openCivPicker.translateY = 30;
        exit.translateX = innerMenus[STRATEGIE.val].width / 2 - exit.width / 2;
        exit.translateY = -25;

        
        for(element in strategieMenu)
        {
            element.fontSize = 14;
            element.txt.hpos = HPos.CENTER;
            element.visible = false;
        }
        cleanDisplay();
        occupationDisplay();
    }

    public function close(): Void
    {
        ViewPrincipal.nextScreen(this);
    }

}
